package when3meet.controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;

import client.ServerAccess;

import when3meet.model.ClientInfo;
import when3meet.view.Application;
import when3meet.view.KickUserButton;
import when3meet.view.MainFrame;
import xml.Message;

public class KickController implements ActionListener {

	KickUserButton kuBtn;

	public KickController(KickUserButton b) {
		kuBtn = b;
	}

	public void process(Message response) {

		Node update = response.contents.getFirstChild();
		NamedNodeMap map = update.getAttributes();

		String completed = map.getNamedItem("completed").getNodeValue();

		// TODO ah really, how could I know which user been removed?!
		if (completed.equals("true")) {

			String user = "name1";
			KickUserButton temp = null;
			for (int i = 0; i < ClientInfo.getInstance().getKickBtn().length; i++) {
				if (ClientInfo.getInstance().getKickBtn()[i].getText().equals(user))
					temp = ClientInfo.getInstance().getKickBtn()[i];
			}
		}

	}

	@Override
	public void actionPerformed(ActionEvent arg0) {

		sendRequest();
	}

	private void sendRequest() {

		ClientInfo ci = ClientInfo.getInstance();

		String xmlString = Message.requestHeader() + "<kickRequest " + "id = '" + ci.getEventID() + "' " + "user = '"
				+ kuBtn.getText().split(" ")[1] + "'/>" + "</request>";

		Message m = new Message(xmlString);

		ServerAccess access = Application.getInstance().getServerAccess();
		access.sendRequest(m);
	}

}
